﻿//********************************************************************************//
//创建日期：<创建日期，2016-4-12>
//页面创建：<孙雪婷，sunxueting@shanghai3h.com>
//后台逻辑：朱大鹏
//功能说明：托收退票
//********************************************************************************//
using System;
using Entity;
using BLL;
using BLL.Access;
using System.Data;

namespace Web.Pages.YongShui._6.ZhangWuGL
{
    public partial class TuoShouTP : BasePage
    {
        #region 属性
        #endregion

        #region 变量
        /// <summary>
        /// 托收明细BLL
        /// </summary>
        YS_SF_TUOSHOUMXBLL BllTuoShou = new YS_SF_TUOSHOUMXBLL();
        /// <summary>
        /// 存储过程访问类
        /// </summary>
        SpHelper sphelper = new SpHelper();
        #endregion

        #region 事件
       /// <summary>
       /// 页面加载
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                InitControl();
            }
        }
        /// <summary>
        /// 确认退票按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQueRen_Click(object sender, EventArgs e)
        {
            QueRen();
        }
        
        /// <summary>
        /// 取消退票按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuXiaoTP_Click(object sender, EventArgs e)
        {
            QuXiao();
        }
        /// <summary>
        /// 前台调用验证事件
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void CallBackControl_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            string msg = "";
            if (e.Parameter == "select")
            {
                SelectXX(out msg);
                e.Result = e.Parameter + "|" + msg;
            }else if (e.Parameter=="queren")
            {
                CheckTJ(out msg);
                e.Result = e.Parameter + "|" + msg;
            }
            else if (e.Parameter == "quxiao")
            {
                msg = CheckQX();
                e.Result = e.Parameter + "|" + msg;
            }
            else if (e.Parameter == "tuipiaotj")
            {
                e.Result = e.Parameter + "|" + QueRen();
            }
            else if (e.Parameter == "qxtuipiaotj")
            {
                e.Result = e.Parameter + "|" + QuXiao();
            }
        }
        #endregion

        #region 方法
        /// <summary>
        /// 初始化默认绑定当前账务年月
        /// </summary>
        private void InitControl()
        {
            txtTuiPiaoRQ.Text= BusinessTool.GetZWYM().ToString();
        }
        /// <summary>
        /// 退票操作
        /// </summary>
        /// <returns></returns>
        private string QueRen()
        {
            string msg = "";
            int ? code=0;
            sphelper.P_YS_SF_TUOSOUTP(Convert.ToInt32(HidValues.Get("TUOSHOUPZH")), LoginInfo.CurrentUser.UserID, cmbTuiPiaoYY.Text, BusinessTool.GetZWYM(), out code);
            if (code > 0)
            {
                
                msg = "1|退票成功";
            }
            else
            {
                
                msg = "1|退票失败";
            }
            return msg;
        }
        /// <summary>
        /// 取消退票操作
        /// </summary>
        /// <returns></returns>
        private string QuXiao()
        {
            string msg = "";
            int? code = 0;
            sphelper.P_YS_SF_QUXIAOTSTP(Convert.ToInt32(HidValues.Get("TUOSHOUPZH")), LoginInfo.CurrentUser.UserID,out code);
            if (code > 0)
            {
                
                msg = "1|取消退票成功";
            }
            else
            {
               
                msg = "1|取消退票失败";
            }
            return msg;
        }
        /// <summary>
        /// 查询用户信息
        /// </summary>
        /// <param name="Msg"></param>
        private void SelectXX(out string Msg)
        {
            Msg = "";
            int ym = 0;
            if (txtYongHuH.Text.Trim().Length == 0)
            {
                Msg = "-1|用户号不能为空";
            }
            else if (txtShuiLiangNY.Text.Trim().Length == 0)
            {
                Msg = "-2|水量年月不能为空";
            }
            else if (!int.TryParse(txtShuiLiangNY.Text.Trim(),out ym))
            {
                Msg = "-3|水量年月格式错误";
            }
            else
            {
                string where = @" S_YONGHUH='{0}' and I_Y*100+I_M={1} 
                                    and exists (select 1 from YS_SF_SHOUFEIXG b where YS_SF_TUOSHOUMX.i_tuoshoupzh = b.I_TSPZH
                                    and b.i_jlzt = 0 and b.i_type in (304, 305) and b.i_zwny = {2}) ";
                where = string.Format(where, txtYongHuH.Text, txtShuiLiangNY.Text.Trim(), txtTuiPiaoRQ.Text);
                DataTable dt = BllTuoShou.SelectYS_SF_TUOSHOUMXesDynamic(where,"",null);
                if (dt != null && dt.Rows.Count > 0)
                {
                   
                    
                    Msg = "1|查询成功|";
                    Msg += dt.Rows[0]["S_YONGHUM"] + "|";
                    Msg += (Convert.ToInt32(dt.Rows[0]["I_Y"]) * 100+Convert.ToInt32(dt.Rows[0]["I_M"])).ToString() + "|";
                    Msg += dt.Rows[0]["S_KAIHUHM"] + "|";
                    Msg += dt.Rows[0]["S_KAIHUZH"] + "|";
                    Msg += dt.Rows[0]["S_KAIHUYH"] + "|";
                    Msg += dt.Rows[0]["N_JINE"] + "|" + dt.Rows[0]["I_TUOSHOUPZH"];
                }
                else
                {
                    Msg = "-4|无相关信息";
                }
            }

        }
        /// <summary>
        /// 退票验证
        /// </summary>
        /// <param name="msg"></param>
        private void CheckTJ(out string msg)
        {
            msg = "";
            int? code = 0;
            int id = 0;
            if (!HidValues.Contains("YongHuH") || !HidValues.Contains("TUOSHOUPZH") || !int.TryParse(HidValues.Get("TUOSHOUPZH").ToString(),out id))
            {
                msg = "-1|请输入用户号和水量年月查询用户相关信息";
            }
            else if (cmbTuiPiaoYY.Value == null)
            {
                msg = "-1|请选择退票原因";
            }
            else if (!int.TryParse(txtTuiPiaoRQ.Text, out id))
            {
                msg = "-1|退票日期格式不正确";
            }
            else
            {
                decimal jine = Convert.ToDecimal(txtJinE.Text);
                string yonghuh = HidValues.Get("YongHuH").ToString();
                int ym = Convert.ToInt32(txtShuiLiangNY2.Text);
                sphelper.P_YS_SF_YANZHENGTSTP(jine, yonghuh, ym, out code);

                if (code > 0)
                {
                    msg = "1|验证通过";
                }
                else if (code == -1)
                {
                    msg = "-1|用户托收信息不存在";
                }
                else if (code == -2)
                {
                    msg = "-1|当前托收凭证未到账，不能退票";
                }
                else if (code == -3)
                {
                    msg = "-1|当前托收凭证已退票，不能退票";
                }
                else if (code == -4)
                {
                    msg = "-1|当前托收凭证未到账";
                }
                else if (code == -5)
                {
                    msg = "-1|当前托收凭证已退票，不能退票";
                }
                else if (code == -6)
                {
                    msg = "-1|当前托收凭证不存在营业账中";
                }
            }
        }
        /// <summary>
        /// 取消退票验证
        /// </summary>
        /// <returns></returns>
        private string CheckQX()
        {
            string msg = "";

            int id = 0;
            if (!HidValues.Contains("YongHuH") || !HidValues.Contains("TUOSHOUPZH") || !int.TryParse(HidValues.Get("TUOSHOUPZH").ToString(), out id))
            {
                msg = "-1|请输入用户号和水量年月查询用户相关信息";
            }
            else
            {
                int? code = 0;
                decimal jine = Convert.ToDecimal(txtJinE.Text);
                string yonghuh = HidValues.Get("YongHuH").ToString();
                int ym = Convert.ToInt32(txtShuiLiangNY2.Text);

                sphelper.P_YS_SF_YANZHENGQXTB(jine, yonghuh, ym, Convert.ToInt32(HidValues.Get("TUOSHOUPZH")), out code);
                if (code > 0)
                {
                    msg = "1|验证通过";
                }
                else if (code == -1)
                {
                    msg = "-1|用户托收信息不存在";
                }
                else if (code == -2)
                {
                    msg = "-1|当前托收凭证未退票";
                }
                else if (code == -3)
                {
                    msg = "-1|当前托收凭证退票信息不存在，不能退票";
                }
            }
            return msg;
        }
        #endregion
        
    }
}